/* 
* File:   Form.h
* Author: Wardell Bagby
* Description:  This file opens a Form controlling the 2nd Spam Filter 
*      application.  
*      File:
*          Open Email:  Runs same method as Button1 below.
* 
*      Button1:  Loads Email.
*      Button2:  Loads the 15 top words and calculates spam percentage of the
*  email.
*      
*/

#pragma once
#include "SpamFilter.h"
namespace SpamFilter {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace Project3;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public

	ref class Form1 : public System::Windows::Forms::Form {
	public:

		Form1(void) {
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>

		~Form1() {
			if (components) {
				delete components;
			}
		}
	private:
		System::Windows::Forms::MenuStrip^ menuStrip1;
	protected:
	private:
		System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
	private:
		System::Windows::Forms::ToolStripMenuItem^ openToolStripMenuItem;
	private:
		System::Windows::Forms::ToolStripMenuItem^ helpToolStripMenuItem;
	private:
		System::Windows::Forms::Button^ button1;
	private:
		System::Windows::Forms::TrackBar^ trackBar1;
	private:
		System::Windows::Forms::Label^ label1;
	private:
		System::Windows::Forms::TextBox^ topWordsText;

	private:
		System::Windows::Forms::TextBox^ emailText;

	private:
		System::Windows::Forms::Label^ label2;
	private:
		System::Windows::Forms::Label^ label3;
	private:
		System::Windows::Forms::ToolStripMenuItem^ loadSpamFilterFileToolStripMenuItem;
	private:
		Project3::SpamFilter^ sF;
	private:
		System::Windows::Forms::Button^ button2;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label5;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>

		void InitializeComponent(void) {
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->openToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->loadSpamFilterFileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->topWordsText = (gcnew System::Windows::Forms::TextBox());
			this->emailText = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->menuStrip1->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar1))->BeginInit();
			this->SuspendLayout();
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->fileToolStripMenuItem, 
				this->helpToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(714, 24);
			this->menuStrip1->TabIndex = 0;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->openToolStripMenuItem, 
				this->loadSpamFilterFileToolStripMenuItem});
			this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
			this->fileToolStripMenuItem->Size = System::Drawing::Size(35, 20);
			this->fileToolStripMenuItem->Text = L"File";
			// 
			// openToolStripMenuItem
			// 
			this->openToolStripMenuItem->Enabled = false;
			this->openToolStripMenuItem->Name = L"openToolStripMenuItem";
			this->openToolStripMenuItem->Size = System::Drawing::Size(169, 22);
			this->openToolStripMenuItem->Text = L"Open Email";
			this->openToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::openToolStripMenuItem_Click);
			// 
			// loadSpamFilterFileToolStripMenuItem
			// 
			this->loadSpamFilterFileToolStripMenuItem->Name = L"loadSpamFilterFileToolStripMenuItem";
			this->loadSpamFilterFileToolStripMenuItem->Size = System::Drawing::Size(169, 22);
			this->loadSpamFilterFileToolStripMenuItem->Text = L"Load SpamFilter File";
			this->loadSpamFilterFileToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::loadSpamFilterFileToolStripMenuItem_Click);
			// 
			// helpToolStripMenuItem
			// 
			this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
			this->helpToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->helpToolStripMenuItem->Text = L"Help";
			this->helpToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::helpToolStripMenuItem_Click);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(12, 482);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(326, 23);
			this->button1->TabIndex = 1;
			this->button1->Text = L"Load SpamFilter File";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// trackBar1
			// 
			this->trackBar1->Enabled = false;
			this->trackBar1->LargeChange = 2;
			this->trackBar1->Location = System::Drawing::Point(18, 434);
			this->trackBar1->Name = L"trackBar1";
			this->trackBar1->Size = System::Drawing::Size(687, 42);
			this->trackBar1->TabIndex = 2;
			this->trackBar1->TickStyle = System::Windows::Forms::TickStyle::TopLeft;
			this->trackBar1->Scroll += gcnew System::EventHandler(this, &Form1::trackBar1_Scroll);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 395);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(70, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Spam Cut-Off";
			// 
			// topWordsText
			// 
			this->topWordsText->Location = System::Drawing::Point(567, 60);
			this->topWordsText->Multiline = true;
			this->topWordsText->Name = L"topWordsText";
			this->topWordsText->ReadOnly = true;
			this->topWordsText->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
			this->topWordsText->Size = System::Drawing::Size(135, 329);
			this->topWordsText->TabIndex = 4;
			// 
			// emailText
			// 
			this->emailText->Location = System::Drawing::Point(15, 63);
			this->emailText->Multiline = true;
			this->emailText->Name = L"emailText";
			this->emailText->ReadOnly = true;
			this->emailText->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
			this->emailText->Size = System::Drawing::Size(542, 329);
			this->emailText->TabIndex = 5;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(15, 44);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(32, 13);
			this->label2->TabIndex = 6;
			this->label2->Text = L"Email";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(564, 44);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(60, 13);
			this->label3->TabIndex = 7;
			this->label3->Text = L"Top Words";
			// 
			// button2
			// 
			this->button2->Enabled = false;
			this->button2->Location = System::Drawing::Point(383, 482);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(319, 23);
			this->button2->TabIndex = 8;
			this->button2->Text = L"Calculate Spam Percentage";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(24, 418);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(13, 13);
			this->label4->TabIndex = 9;
			this->label4->Text = L"0";
			this->label4->Click += gcnew System::EventHandler(this, &Form1::label4_Click);
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(683, 418);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(19, 13);
			this->label5->TabIndex = 10;
			this->label5->Text = L"10";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->AutoSize = true;
			this->ClientSize = System::Drawing::Size(714, 517);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->emailText);
			this->Controls->Add(this->topWordsText);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->trackBar1);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Spam Percentage Calculator";
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private:
		/**Either loads the SpamFilter file or loads the email, depending on whether or not a SpamFilter file has been loaded yet.**/
		System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
			if(button1->Text->Equals("Load SpamFilter File"))
			{
				Stream^ myStream;
				OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

				openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
				openFileDialog1->FilterIndex = 2;
				openFileDialog1->RestoreDirectory = true;

				if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
					if ((myStream = openFileDialog1->OpenFile()) != nullptr) {
						myStream->Close();
						sF = gcnew Project3::SpamFilter();
						if (sF->setSpamFile(File::Open(openFileDialog1->FileName, FileMode::Open)))
						{
							button1->Enabled = true;
							openToolStripMenuItem->Enabled = true;
							trackBar1->Enabled = true;
						}
						button1->Text = "Load Email File";
					} else {
						//Insert code to see tell user to load valid Spam File.
					}
				}
			}
			else
			{
				Stream^ myStream;
				OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

				openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
				openFileDialog1->FilterIndex = 2;
				openFileDialog1->RestoreDirectory = true;
				trackBar1->Enabled = true;
				button2->Enabled = true;
				trackBar1->Value = sF->getMedian();
				if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
					if ((myStream = openFileDialog1->OpenFile()) != nullptr) {
						try {
							sF->readInEmail((FileStream^)myStream);
							emailText->Text = sF->getEmail()->getBody();
						} catch (Exception^ e) {
							emailText->Text = e->Message;
						}
						myStream->Close();
					} else {
						//Insert code to see tell user to load valid Email File.
					}
				}
			}
		};

	private:
		/**Changes the median depending on the position of the trackbar.**/
		System::Void trackBar1_Scroll(System::Object^ sender, System::EventArgs^ e) {
			if (sF != nullptr)
				sF->setMedian(trackBar1->Value);
		}
	private:
		/**Loads the SpamFilter file.**/
		System::Void loadSpamFilterFileToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
			Stream^ myStream;
			OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

			openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
			openFileDialog1->FilterIndex = 2;
			openFileDialog1->RestoreDirectory = true;

			if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
				if ((myStream = openFileDialog1->OpenFile()) != nullptr) {
					myStream->Close();
					sF = gcnew Project3::SpamFilter();
					if (sF->setSpamFile(File::Open(openFileDialog1->FileName, FileMode::Open)))
					{
						button1->Enabled = true;
						openToolStripMenuItem->Enabled = true;
						trackBar1->Enabled = true;
					}
					openToolStripMenuItem->Enabled = true;
					button1->Text = "Load Email File";
				} else {
					//Insert code to see tell user to load valid Spam File.
				}
			}
		}
	private:
		/**Loads an email.**/
		System::Void openToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
			button1_Click(sender, e);
		}
	private:
		/**Calculates the Spam Percentage and displays the Top Words.**/
		System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
			array <String^>^ topWordsWithPercent = sF->calculateProbablity(sF->getEmail());
			double spamPercentage = Convert::ToDouble(topWordsWithPercent[15])*1000000000;
			array<String^>^ topWords = gcnew array<String^>(15);
			for(int i=0; i<topWords->Length; i++)
				topWords[i] = topWordsWithPercent[i];
			topWordsText->Lines = topWords;
			String^ message = "";
			if(spamPercentage >= sF->getMedian())
			{
				message = "This email is spam with a percentage of: "+spamPercentage;
			}
			else
				message = "This email is ham with a percentage of: "+spamPercentage;
			MessageBox::Show(nullptr,message,"Spam Percentage");
		}
	private:
		/**Shows the help dialog.**/
		System::Void helpToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
			MessageBox::Show("To start, load a SpamFilter File (made with the Repository application) using the button in the bottom left.\n\nNext, load up an email file (purely textual, with only the body in the file) and click \"Calculate Spam Percentage.\"\n\nA dialog box will appear telling you the spam percentage, and a text box will be filled with the words that were the most extreme in this email.\n\nThe trackbar at the bottom is used as the cut-off bar for Spam messages. Everything above the percentage set there will be considered Spam, while everything below will be considered Ham.");

		}
		/**Secret method capable of unlimited power. Upgrades any computer to have unlimited RAM, 10.09GHZ Hexacore CPU, and a Petabyte hard drive. But you have to run Windows 3.1.**/
	private: System::Void label4_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	};
}

